package net.mindView.threads;

import java.util.concurrent.TimeUnit;

//方式1：直接继承Thread类
public class Threads extends Thread {
    private int countDown = 5;
    private static int threadCount = 0;
    public Threads(){
        super(Integer.toString(++threadCount));
        start();
    }
    public void run(){
        while(true){
            System.out.println(this);
            if(--countDown == 0)
                return;
        }
    }
}

//方式2：实现Runnable接口
class SelfManaged implements Runnable{
    private int countDown = 5;
    private Thread t = new Thread(this);
    public SelfManaged(){ t.start(); }
    public void run(){
        while(true){
            System.out.println(this);
            if(--countDown == 0)
                return;
        }
    }
}

//方式3：创建继承Thread的内部类
class InnerThread1{
    private int countDown = 5;
    private Inner inner;
    private class Inner extends Thread{
        Inner(String name){
            super(name);
            start();
        }
        public void run(){
            try{
                while(true){
                    System.out.println(this);
                    if(--countDown == 0)
                        return;
                    sleep(10);
                }
            }catch (InterruptedException e){
                System.out.println(e);
            }
        }
    }
    public InnerThread1(String name){
        inner = new Inner(name);
    }
}

//方式4：创建匿名内部类
class InnerThread2{
    private int countDown = 5;
    private Thread t;
    public InnerThread2(String name){
        t = new Thread(name){
            public void run(){
                try{
                    while(true){
                        System.out.println(this);
                        if(--countDown == 0)
                            return;
                        sleep(10);
                    }
                }catch (InterruptedException e){
                    System.out.println(e);
                }
            }
        };
        t.start();
    }
}

//方式5：创建实现了Runnable接口的内部类
class InnerRunnable1{
    private int countDown = 5;
    private Inner inner;
    private class Inner implements Runnable{
        Thread t;
        Inner(String name){
            t = new Thread(this,name);
            t.start();
        }
        public void run(){
            try{
                while(true){
                    System.out.println(this);
                    if(--countDown == 0)
                        return;
                    TimeUnit.MILLISECONDS.sleep(100);
                }
            }catch (InterruptedException e){
                System.out.println(e);
            }
        }
    }
    public InnerRunnable1(String name){
        inner = new Inner(name);
    }
}

//方式6：创建实现了Runnable接口的匿名内部类
class InnerRunnable2{
    private int countDown = 5;
    private Thread t;
    public InnerRunnable2(String name){
        t = new Thread(new Runnable() {
            @Override
            public void run(){
                try{
                    while(true){
                        System.out.println(this);
                        if(--countDown == 0)
                            return;
                        TimeUnit.MILLISECONDS.sleep(100);
                    }
                }catch (InterruptedException e){
                    System.out.println(e);
                }
            }
        },name);
        t.start();
    }
}

//方式7：在一个方法中开启一个线程
class ThreadMethod{
    private int countDown = 5;
    private Thread t;
    private String name;
    public ThreadMethod(String name){ this.name = name; }
    public void runTask(){
        if(t==null){
            t = new Thread(name){
                public void run(){
                    try{
                        while(true){
                            System.out.println(this);
                            if(--countDown == 0)
                                return;
                            sleep(100);
                        }
                    }catch (InterruptedException e){
                        System.out.println(e);
                    }
                }
            };
            t.start();
        }
    }
}
